package com.aaa.utils;

import com.aaa.service.MouseService;
import com.aaa.service.MouseServiceImpl;
import net.sf.cglib.proxy.InvocationHandler;
import net.sf.cglib.proxy.Proxy;

import javax.xml.crypto.Data;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author yudejie2020
 */
public class MyDynamicProxyInterface implements InvocationHandler {
    private Object targetObj;


    public Object getProxyObj(Object target){
        targetObj=target;
        Object proxyInstance=Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),this);
        return proxyInstance;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //增强处理
        System.out.println("日志："+method.getName()+"执行了"+new Date());
        //执行目标对象的方法
        Object invoke=method.invoke(targetObj,args);

        return invoke;
    }

    public static void main(String[] args) {
        //目标对象
        MouseService mouseService=new MouseServiceImpl();
        //获取代理对象
        MyDynamicProxyInterface myDynamicProxyInterface=new MyDynamicProxyInterface();
        MouseService proxyObj=(MouseService) myDynamicProxyInterface.getProxyObj(mouseService);
        proxyObj.buyMouse(300);
    }
}
